using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Controls;
using System.Windows.Markup;
namespace Microsoft.Expression.Interactivity
{
	internal static class TypeConverterHelper
	{
		private class ExtendedStringConverter : TypeConverter
		{
			private Type type;
			public ExtendedStringConverter(Type type)
			{
				this.type = type;
			}
			public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
			{
				return (typeof(IConvertible).IsAssignableFrom(this.type) && typeof(IConvertible).IsAssignableFrom(destinationType)) || base.CanConvertTo(context, destinationType);
			}
			public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
			{
				return sourceType == typeof(string) || sourceType == typeof(uint) || base.CanConvertFrom(context, sourceType);
			}
			public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
			{
				IConvertible convertible = value as IConvertible;
				if (convertible != null && typeof(IConvertible).IsAssignableFrom(destinationType))
				{
					return convertible.ToType(destinationType, CultureInfo.get_InvariantCulture());
				}
				return base.ConvertTo(context, culture, value, destinationType);
			}
			public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
			{
				string text = value as string;
				if (text != null)
				{
					Type type = this.type;
					Type underlyingType = Nullable.GetUnderlyingType(type);
					if (underlyingType != null)
					{
						if (string.Equals(text, "null", 5))
						{
							return null;
						}
						type = underlyingType;
					}
					else
					{
						if (type.get_IsGenericType())
						{
							return base.ConvertFrom(context, culture, value);
						}
					}
					object obj = new object();
					object obj2 = obj;
					if (type == typeof(bool))
					{
						obj2 = bool.Parse(text);
					}
					else
					{
						if (type.get_IsEnum())
						{
							obj2 = Enum.Parse(this.type, text, false);
						}
						else
						{
							StringBuilder stringBuilder = new StringBuilder();
							string text2 = "clr-namespace:" + type.get_Namespace() + ";assembly=" + type.get_Assembly().get_FullName().Split(new char[]
							{
								','
							})[0];
							stringBuilder.Append("<ContentControl xmlns='http://schemas.microsoft.com/client/2007' xmlns:c='" + text2 + "'>\n");
							stringBuilder.Append("<c:" + type.get_Name() + ">\n");
							stringBuilder.Append(text);
							stringBuilder.Append("</c:" + type.get_Name() + ">\n");
							stringBuilder.Append("</ContentControl>");
							ContentControl contentControl = XamlReader.Load(stringBuilder.ToString()) as ContentControl;
							if (contentControl != null)
							{
								obj2 = contentControl.get_Content();
							}
						}
					}
					if (obj2 != obj)
					{
						return obj2;
					}
				}
				else
				{
					if (value is uint)
					{
						if (this.type == typeof(bool))
						{
							return (uint)value != 0u;
						}
						if (this.type.get_IsEnum())
						{
							return Enum.Parse(this.type, Enum.GetName(this.type, value), false);
						}
					}
				}
				return base.ConvertFrom(context, culture, value);
			}
		}
		internal static object DoConversionFrom(TypeConverter converter, object value)
		{
			object result = value;
			try
			{
				if (converter != null && value != null && converter.CanConvertFrom(value.GetType()))
				{
					result = converter.ConvertFrom(value);
				}
			}
			catch (Exception e)
			{
				if (!TypeConverterHelper.ShouldEatException(e))
				{
					throw;
				}
			}
			return result;
		}
		private static bool ShouldEatException(Exception e)
		{
			bool flag = false;
			if (e.get_InnerException() != null)
			{
				flag |= TypeConverterHelper.ShouldEatException(e.get_InnerException());
			}
			return flag | e is FormatException;
		}
		internal static TypeConverter GetTypeConverter(Type type)
		{
			TypeConverterAttribute typeConverterAttribute = (TypeConverterAttribute)Attribute.GetCustomAttribute(type, typeof(TypeConverterAttribute), false);
			if (typeConverterAttribute != null)
			{
				TypeConverter result;
				try
				{
					Type type2 = Type.GetType(typeConverterAttribute.get_ConverterTypeName(), false);
					if (type2 == null)
					{
						goto IL_3F;
					}
					result = (Activator.CreateInstance(type2) as TypeConverter);
				}
				catch
				{
					goto IL_3F;
				}
				return result;
			}
			IL_3F:
			return new TypeConverterHelper.ExtendedStringConverter(type);
		}
	}
}
